package com.lgx.mobile.rest.friend;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.mobile.model.UserModel;
import com.lgx.right.entity.User;
import com.lgx.right.entity.UserExpand;
import com.lgx.right.service.IUserExpandService;
import com.lgx.right.service.IUserService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/getFriendMailList")
public class GetFriendMailListController {
	
	private static final Logger logger = Logger.getLogger(GetFriendMailListController.class);

	@Autowired
	IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}
	
	@Autowired
	IUserExpandService userExpandService;

	public IUserExpandService getUserExpandService() {
		return userExpandService;
	}

	/**
	 * 乐友通讯录查询
	 * @param userId id is key
	 * @return 距离1000米以内，除自己外用户信息集合；0：该用户不存在；1：参数异常，有参数为空；2：没有该用户的扩展信息
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Object getFriendMailList(@PathVariable("id") String userId){
		if(StringUtils.isNotBlank(userId)){
			List<UserModel> userModelList = new ArrayList<UserModel>();
			User user = getUserService().findUserById(Long.parseLong(userId));
			if(user!=null){
				UserExpand userExpand = getUserExpandService().findUserExpandByUserId(user.getId());
				if(userExpand != null){
					int x = Integer.parseInt(userExpand.getPoints().split(",")[0]);
					int y = Integer.parseInt(userExpand.getPoints().split(",")[1]);
					List<UserExpand> userExpandList = getUserExpandService().findUserExpandListByPoints(Long.parseLong(userId), x, y);
					for(int i=0;i<userExpandList.size();i++){
						User friendUser = getUserService().findUserById(userExpandList.get(i).getUser().getId());
						UserModel userModel = new UserModel(null);
						userModel.setDegree(userExpandList.get(i).getDegree());
						userModel.setFunType(userExpandList.get(i).getFunType());
						userModel.setLevel(userExpandList.get(i).getLevel());
						userModel.setGiftRecord(userExpandList.get(i).getGiftRecord());
						userModel.setId(friendUser.getId());
						userModel.setCardId(friendUser.getCard().getId());
						userModel.setTrueName(friendUser.getTruename());
						userModel.setScore(friendUser.getCard().getScore());
						userModelList.add(userModel);
					}
					return userModelList;
				}else{
					if(logger.isDebugEnabled()){
						logger.debug("getFriendMailList faild please check userExpand is exists");
					}
					return "2";
				}
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("getFriendMailList faild id is error");
				}
//				"getFriendMailList faild please check id is exists";
				return "0";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("getFriendMailList faild id is not filled");
			}
//		    "getFriendMailList faild please check id is filled";
			return "1";
		}
	}
}
